package com.partners.controller.Page;

import com.alibaba.fastjson.JSONObject;
import com.partners.annotation.Auths;
import com.partners.constant.PermissionEnum;
import com.partners.service.CustomerService;
import com.partners.service.UserService;
import com.partners.utils.CodeUtils;
import com.partners.utils.SessionManager;
import lombok.extern.slf4j.Slf4j;
import org.bouncycastle.math.raw.Mod;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @author liangyongle
 * @date 2018-12-17
 */
@Controller
@RequestMapping("customerPage")
@Slf4j
public class CustomerPageController {


    @Resource
    private CustomerService customerService;

    @Resource
    private SessionManager sessionManager;

    @Resource
    private UserService userService;



    /**
    * 新建报备
    * */
    @GetMapping("addCustomer")
    @Auths
    public ModelAndView addCustomer(HttpSession session){
        JSONObject jsonObject = sessionManager.JsonSession(session);
        Integer uid = jsonObject.getInteger("userId");
        Integer code = PermissionEnum.ADD_CUSTOMER.getCode();
        boolean permission = userService.hasPermission(uid, code);
        String viewName = "show/addCustomer";
        ModelAndView mav = CodeUtils.checkPermissionView(permission, viewName, code, uid);
        mav.addObject("uid",uid);
        return mav;
    }



    /**
     * 报备记录
     *
    * */
    @GetMapping("customerList")
    @Auths
    public ModelAndView getAllcustomerList(HttpSession session){
        JSONObject jsonObject = sessionManager.JsonSession(session);
        Integer uid = jsonObject.getInteger("userId");
        //查询是否有查询全部的权限
        Integer code = PermissionEnum.ALL_CUSTOMER_LIST.getCode();
        boolean permission = userService.hasPermission(uid, code);
        Integer isall = 0;
        if(permission){isall = 1;}
        ModelAndView mav =new ModelAndView("show/customer_list");
        mav.addObject("uid",uid);
        mav.addObject("isall",isall);
        return mav;
    }

    /**
     * 报备详情
     * */
    @GetMapping("customerDetail/{id}")
    public ModelAndView customerDetail(@PathVariable("id") Integer Id){
        ModelAndView mav = new ModelAndView("show/customer_detail");
        mav.addObject("id",Id);
        return mav;
    }


    /**
     * 优惠券页面
     * */
    @GetMapping("getCustomerCode/{id}")
    public ModelAndView getCouponCode(@PathVariable("id") Integer Id){
        ModelAndView mav = new ModelAndView("show/customer_code");
        Map<String,Object> detail = customerService.getCustomerQrCode(Id);
        mav.addObject("detail",detail);
        return mav;
    }


    /**
    * 扫描优惠券二维码
    *
    * */
    @GetMapping("scan_code")
    @Auths
    public ModelAndView scanCode(HttpSession session,@RequestParam(value="customer_id", required=false) Integer id){
        Integer type = customerService.scanCode(session,id);
        JSONObject jsonObject = sessionManager.JsonSession(session);
        Integer uid = jsonObject.getInteger("userId");
        //查询是否有核销权限
        Integer code = PermissionEnum.VERIFICATION_COUPON.getCode();
        boolean permission = userService.hasPermission(uid, code);
        log.info("permission:{}",permission);
        ModelAndView mav = new ModelAndView();
        if(permission){
            mav.setViewName("show/verification_code");
            mav.addObject("id",id);
            mav.addObject("uid",uid);
        }else{
            mav.setViewName("show/scan_code");
            mav.addObject("id",id);
            mav.addObject("type",type);
        }

        //有就打开核销页面
        return mav;
    }


    /**
     * 获取成交客户记录
     * @param
     * @return
     * */
    @GetMapping("orderCustomer")
    @Auths
    public ModelAndView orderCustomer(HttpSession session){
        JSONObject jsonObject = sessionManager.JsonSession(session);
        Integer uid = jsonObject.getInteger("userId");
        Integer code = PermissionEnum.GET_ORDER_CUSTOMER.getCode();
        boolean permission = userService.hasPermission(uid, code);
        String viewName = "show/order_customer";
        ModelAndView mav = CodeUtils.checkPermissionView(permission, viewName, code, uid);
        mav.addObject("uid",uid);
        return mav;
    }

    /**
     * 获取成交客户的记录
     * @param
     * @return
     */
    @GetMapping("customer_log/{id}")
    @Auths
    public ModelAndView getOrderCustomerLog(@PathVariable("id") Integer id,HttpSession session){
        JSONObject jsonObject = sessionManager.JsonSession(session);
        Integer uid = jsonObject.getInteger("userId");
        Integer code = PermissionEnum.GET_ORDER_CUSTOMER.getCode();
        boolean permission = userService.hasPermission(uid, code);
        String viewName = "show/customer_log";
        ModelAndView mav = CodeUtils.checkPermissionView(permission, viewName, code, uid);
        mav.addObject("uid",uid);
        mav.addObject("id",id);
        return mav;
    }






}
